<?php
namespace App\Http\Controllers\Front;

use App\Service\Front\CasexqService;
use Illuminate\Http\Request;

class CasexqController extends BaseController
{
    //原创案例-上传

    public function __construct(CasexqService $caseService)
    {
        $this->db = $caseService;
    }

    //选项分类
    public function getCateList(Request $request)
    {
        header("Access-Control-Allow-Origin: *");
        $pid = $request->post('pid', 0);
        $res = $this->db->getCateList($pid);
        return $this->toJson(0, '', $res);
    }

    //列表
    public function getIndex(Request $request)
    {
        header("Access-Control-Allow-Origin: *");
        $cateid = $request->post('cate_id', 0);
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $pages['start'] = $pages['start'] ? $pages['start'] : 1;
        $pages['limit'] = $pages['limit'] ? $pages['limit'] : 10;
        $res = $this->db->getIndex($cateid, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function getOne(Request $request)
    {
        header("Access-Control-Allow-Origin: *");
        $token = $request->post('token', '');
        $id = $request->post('id', 0);
        $uid = $this->db->uidByToken($token);
        $res = $this->db->getOne($id, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

//    //添加
//    public function add(Request $request)
//    {
//        header("Access-Control-Allow-Origin: *");
//        $token = $request->post('token', '');
//        $pas['cid'] = $this->db->cidByToken($token);
//        $pas['title'] = $request->post('title', '');
//        $pas['cate_id'] = $request->post('cate_id', 0);
//        $pas['address'] = $request->post('address', '');
//        $pas['buyer_name'] = $request->post('buyer_name', '');
//        $pas['designer_name'] = $request->post('designer_name', '');
//        $pas['item_area'] = $request->post('item_area', 0);
//        $pas['main_material'] = $request->post('main_material', '');
//        $pas['item_finish_time'] = $request->post('item_finish_time', '');
//        $pas['thumb'] = $request->post('thumb', '');
//        $pas['item_detail'] = $request->post('item_detail', '');
//        if (!$pas['cid']) {
//            return $this->toJson(-2, '你的企业身份无效');
//        }
//        if (!$pas['title']) {
//            return $this->toJson(-2, '项目名称必须的');
//        }
//        $res = $this->db->add($pas);
//        if ($res['code'] < 0) {
//            return $this->toJson(-2, $res['msg']);
//        }
//        return $this->toJson();
//    }

    //相似需求
    public function getAlmostList(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $pages['start'] = $pages['start'] ? $pages['start'] : 1;
        $pages['limit'] = $pages['limit'] ? $pages['limit'] : 10;
        $cate_id = $request->post('cate_id', 0);
        $res = $this->db->getAlmostList($cate_id, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //设计师
    public function getDesignerList(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $pages['start'] = $pages['start'] ? $pages['start'] : 1;
        $pages['limit'] = $pages['limit'] ? $pages['limit'] : 10;
        $res = $this->db->getDesignerList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //收藏设置
    public function setCollect(Request $request)
    {
        $token = $request->post('token', '');
        $case_id = $request->post('case_id', 0);
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '未登录');
        }
        if (!$case_id) {
            return $this->toJson(-2, '案例必须的');
        }
        $res = $this->db->setCollect($uid, $case_id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //转发设置
    public function setForward(Request $request)
    {
        $token = $request->post('token', '');
        $case_id = $request->post('case_id', 0);
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '未登录');
        }
        if (!$case_id) {
            return $this->toJson(-2, '案例必须的');
        }
        $res = $this->db->setForward($uid, $case_id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //反馈
    public function setJubao(Request $request)
    {
        $token = $request->post('token', '');
        $case_id = $request->post('case_id', 0);
        $cnt = $request->post('detail', 0);
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '未登录');
        }
        if (!$case_id) {
            return $this->toJson(-2, '案例必须的');
        }
        if (!$cnt) {
            return $this->toJson(-2, '内容必须的');
        }
        $res = $this->db->setJubao($uid, $case_id, $cnt);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //发布类似需求
    public function setAlmost(Request $request)
    {
        $token = $request->post('token', '');
        $id = $request->post('id', 0);
        $cid = $this->db->cidByToken($token);
        if (!$cid) {
            return $this->toJson(-2, '公司不存在');
        }
        $res = $this->db->setAlmost($id, $cid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //接单申请
    public function orderApply(Request $request)
    {
        $token = $request->post('token', '');
        $caseid = $request->post('case_id', 0);
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->orderApply($uid, $caseid);
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson();
    }
}